﻿using System;
using Microsoft.Practices.Unity;

using SharedGenomics.Core.Configuration;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Annotations.Requests;

namespace SharedGenomics.Workbench.Core.Configuration
{
    public class AnnotationContainerConfigurationStrategy : IContainerConfigurationStrategy
    {
        public void Configure(ContainerConfigurationContext context)
        {
            var container = context.Container;

            container.RegisterType<IAnnotationRepositoryMediator, AnnotationRepositoryMediator>(new ContainerControlledLifetimeManager());
            container.RegisterType<IAnnotationRequestStatusNotifier, AnnotationRequestStatusNotifier>(new ContainerControlledLifetimeManager());
            container.RegisterType<IAnnotationRequestBuilderFactory, AnnotationRequestBuilderFactory>();
            container.RegisterType<IAnnotationPlanFactory, StaticAnnotationPlanFactory>();
            container.RegisterType<IAnalysisResultSetAnnotator, ResultSetAnnotator>();
            container.RegisterType<ITavernaClient, TavernaClient>();

            //setup annotation manager - only have google search annotations as active
            container.RegisterType<IAnnotationManager, AnnotationManager>(new ContainerControlledLifetimeManager());

            //setup request pipeline
            AnnotationRequestPipeline pipeline = new AnnotationRequestPipeline();
            pipeline.AddRequestStage<AggregateAnnotationRequestStage>();

            var lookupStage = container.Resolve<LookupAnnotationRequestStage>();
            pipeline.AddRequestStage(lookupStage);

            var executeStage = container.Resolve<ExecuteAnnotationRequestStage>();
            pipeline.AddRequestStage(executeStage);

            var updateStage = container.Resolve<RepositoryUpdateAnnotationRequestStage>();
            pipeline.AddRequestStage(updateStage);

            var notificationStage = container.Resolve<NotificationAnnotationRequestStage>();
            pipeline.AddRequestStage(notificationStage);

            pipeline.Fix();
            container.RegisterInstance(pipeline, new ContainerControlledLifetimeManager());

            container.RegisterType<AnnotationRequestQueue>(new ContainerControlledLifetimeManager());

            //request types
            container.RegisterType<NcbiAnnotationRequest>(new ContainerControlledLifetimeManager());
            container.RegisterType<TavernaAnnotationRequest>(new ContainerControlledLifetimeManager());
        }
    }
}
